# -*- coding: utf-8 -*-
import td_const, td_cmd, td_error
#房间内玩家对象类
class CPlayer(object):
	def __init__(self, objRoom, _player, iSide):
		super(CPlayer, self).__init__()
		self.objRoom = objRoom				#房间对象的引用，某些历史数据的提交需要用到
		self.objHallplayer = _player	#大厅玩家对象的引用，某些数据的获取和刷新需要用到
		self.iSide = iSide						#玩家唯一标识
		self.hid = _player.hid				#玩家socket
		self.uid = _player.uid				#玩家uid
		self.urs = _player.username		#玩家账号
		self.ip = _player.ip					#登陆客户端的ip
		self.nickname = _player.nickname	#玩家昵称
		self.gmlvl = _player.gmlvl		#玩家gm等级
		self.avatar = _player.avatar	#玩家个人avatar信息
		self.pet = _player.pet				#玩家宠物avatar信息
		
		self.score = _player.score		#玩家的游戏积分
		self.win_count = _player.win_count	#玩家的胜利局数
		self.draw_count = _player.draw_count	#玩家的平局数
		self.lose_count = _player.lose_count	#玩家的失败局数
		self.break_count = _player.break_count	#玩家的掉线（逃跑）局数
		#self.yuanbao = _player.ybpay		#玩家的冲值游戏币
		#self.yuanbao_free = _player.ybfree	#玩家的赠送游戏币
		self.yuanbao = 0
		self.yuanbao_free = 0
		#记录玩家登陆时候的基本信息日志
		text = "[PLAYER_ENTER][COMMON_DATA][urs=%s;uid=%d;ip=%s;side=%d;score=%d;gmlvl=%d;win=%d;lose=%d;break=%d;yb=%d;ybfree=%d]" % \
		(self.urs, self.uid, self.ip, self.iSide, self.score, self.gmlvl, self.win_count, self.lose_count, self.break_count, self.yuanbao, self.yuanbao_free)
		self.objRoom.cglog(td_const.LOGTYPE_KEYDATA, text)
		
		self.iBuyTag = None			#购买道具的tag，向数据库请求购买道具的时候用于区分异步返回的购买请求到底是哪个
		self.bIsReady = False		#是否准备的标志位
		self.bIsInit = False		#是否初始化完成的标志位
		self.lstInitCard = []		#选择带入游戏的卡片
		self.iSelectStage = 1		#选中的关卡
		#读取记录在buff中的游戏历史数据
		#self.read_bufs(_player)
		self.lstFinishAchieveID = [] #已经完成的成就id
		self.lstLeaveAchieveID = [] #未完成的成就id
		#读取成就数据
		self.read_achieve_finish()
		
	#此函数获得已完成成就的id
	def read_achieve_finish(self):
		self.lstFinishAchieveID = []
		achmap = self.objHallplayer.achmap
		self.lstFinishAchieveID = achmap.keys()
	def clean(self):
		self.objHallplayer = None
		self.objRoom = None
	def on_game_start(self):
		pass
	#此函数在游戏结算的时候调用，保存和更新必要的历史数据
	def on_game_end(self, stage, game_player):
		#更新已完成关卡列表并写buff
		if game_player.bWin and (not stage in self.lstFinishStage):
			self.lstFinishStage.append(stage)
			self.write_bufs(td_const.BUFF_COMPLETE)
		#更新胜负局数
		if game_player.bWin:
			self.win_count += 1
			self.objRoom.cghall_inc_win_count(self.hid)
		else:
			self.lose_count += 1
			self.objRoom.cghall_inc_lose_count(self.hid)
		#更新积分数据
		self.score += game_player.iScore
		self.objRoom.cghall_inc_score(self.hid, game_player.iScore)
		#检测成就完成情况
		self.check_achievement(stage, game_player)
		#游戏结算时写当局结算结果日志
		self.objRoom.cglog(td_const.LOGTYPE_KEYDATA, "[GAME_END][COMMON_DATA][uid=%s;bWin=%d;dScore=%d]" % (self.uid, game_player.bWin, game_player.iScore))
		self.objRoom.cglog(td_const.LOGTYPE_KEYDATA, "[GAME_END][CUSTOM_DATA][uid=%s;finish_stage=%s]" % (self.uid, str(self.lstFinishStage)))
		
		pass
	##更用户的成就
	def update_achieve(self):
		pass